<template>
  <view class="adress">
    <!-- 选择收货地址 -->
    <view class="choose" v-if="show">
      <button type="primary" size="mini" @click="chooseAdress">请选择收货地址+</button>
    </view>
    <!-- 收货地址信息 -->
    <view class="address-info-box" v-else @click="chooseAdress">
        <view class="row1">
          <view class="row1-left">
            <view class="username">收货人：<text>{{adress.userName}}</text></view>
          </view>
          <view class="row1-right">
            <view class="phone">电话：<text>{{adress.telNumber}}</text></view>
            <uni-icons type="arrowright" size="16"></uni-icons>
          </view>
        </view>
        <view class="row2">
          <view class="row2-left">收货地址：</view>
          <view class="row2-right">{{fullAdress}}</view>
        </view>
      </view>
    <!-- 分割线 -->
    <image src="../../static/tab_icons/adressline.jpg" class="image"></image>
  </view>
</template>

<script>
  import {mapState,mapGetters,mapMutations} from "vuex"
  export default {
    name:"my-adress",
    data() {
      return {
      };
    },
    computed:{
      ...mapState('user',['adress']),
      ...mapGetters('user',['fullAdress']),
      show(){
        const flag = Object.keys(this.adress).length === 0
        return flag
      }
    },
    methods:{
      ...mapMutations('user',['setAdress']),
      async chooseAdress() {
        const [err,suc] = await uni.chooseAddress().catch(e=> console.log(e))
        // console.log(suc);
        if(err === null && suc.errMsg === 'chooseAddress:ok') {
          this.setAdress(suc)
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.adress {
  background-color: #fff;
  .choose {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .image {
    width: 750rpx;
    height: 5px;
  }
  
  // 渲染收货信息的盒子
  .address-info-box {
    font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
  
    // 第一行
    .row1 {
      display: flex;
      justify-content: space-between;
  
      .row1-right {
        display: flex;
        align-items: center;
  
        .phone {
          margin-right: 5px;
        }
      }
    }
  
    // 第二行
    .row2 {
      display: flex;
      align-items: center;
      margin-top: 10px;
  
      .row2-left {
        white-space: nowrap;
      }
    }
  } 
}
</style>